<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class ProsoccerParser  extends CI_Controller{
	
	
	public function parseTeams($teamString)
	{
		$teams = explode(" - ", $teamString);
		return $teams;
	}
	
	public function massReplace($proc)
	{
		$proc = str_replace("i1/G2.gif", "7", $proc);
		$proc = str_replace("i1/K2.gif", "4", $proc);
		$proc = str_replace("i1/L2.gif", "5", $proc);
		$proc = str_replace("i1/A2.gif", "1", $proc);
		$proc = str_replace("i1/H2.gif", "2", $proc);
		$proc = str_replace("i1/D2.gif", "6", $proc);
		$proc = str_replace("i1/C2.gif", "3", $proc);
		$proc = str_replace("i1/E2.gif", "0", $proc);
		$proc = str_replace("i1/B2.gif", "9", $proc);
		$proc = str_replace("i1/F2.gif", "8", $proc);
		$proc = str_replace("i1/M.gif", "", $proc);		
		return $proc;
	}
	
	public function parseTip($tip)
	{
		$tip = str_replace("im/1B.gif", "1", $tip);
		$tip = str_replace("im/21.gif", "21", $tip);
		$tip = str_replace("im/X1.gif", "X1", $tip);
		$tip = str_replace("im/1X.gif", "1X", $tip);
		$tip = str_replace("im/2X.gif", "2X", $tip);
		$tip = str_replace("im/2B.gif", "2", $tip);
		$tip = str_replace("im/12.gif", "12", $tip);
		
		
		//echo $tip;
		return $tip;
	}
	
	public function parseDate($dateString)
	{
		
		// $dateString =substr( $dateString, 1 );
		$dateString=trim($dateString);
		 // Friday 31-Aug
		 //absmiddle !!!!!!!!!!!
		 
		///echo $dateString;
		return  $dateString;
	}
	
	
    public function parseMainPage($page)
    {
    	$rowNo=0;
    	
    	
    	// import simple html dom lib
    	$this->load->library('simple_html_dom');
    	// parse page to DOM
    	///////////$raw = file_get_html($page);
    	//$string = file_get_contents(($page)
    	//$raw = new simple_html_dom();
    	//$raw->load($string); // Load HTML from a string
    	
    	$curl = curl_init();
    	curl_setopt($curl, CURLOPT_URL, $page);
    	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
    	$str = curl_exec($curl);
    	curl_close($curl);
    	
    	$raw= str_get_html($str);
    	
    	
    	
    	
    	
    	
    	
    	$arrayOfGames;
    	$i=0;
    	
    	$this->load->model('game');
    	$game= new Game();
    	
    	// uzimanje datuma sa stranice
    	$dateRaw = $raw->find('td[class=R5]');
    	$dayMonth = explode(" | ",$dateRaw[0]->innertext);   
    	// proci kroz sedam elemenata i naci najveci
    	$duzina =0;
    	$deo;
    	for ($i=0;$i<6;$i++){
    		if (strlen($dayMonth[$i])>$duzina){
    			$duzina=strlen($dayMonth[$i]);
    			$deo=$dayMonth[$i];
    		}
    	} 	
    	$rawData =  str_get_html($deo);    	
    	$deo=$rawData->plaintext;
    	$dateFromPage =$this->parseDate($deo);
    	
    	foreach($raw->find('tr[class=f2]') as $element){
    		//echo $element->outertext . '<br>';    			
    		
    		$game= new Game(); 
    		// parsiranje timova   		
    		$teams = $this->parseTeams($element->children[1]->plaintext);
    		// parsiranje procenata   		
    		$procenatFix1="";
    		if(array_key_exists('0', $element->children[2]->children)){
    			$proc = $element->children[2]->children[0]->src; 
    						
    			$proc=$this->massReplace($proc);
    			$procenatFix1 = $procenatFix1.$proc;
    		}
    		else {
    			$proc = $element->children[2];    			
    			$procenatFix1 = $procenatFix1.$proc;
    		}
    	
    		if(array_key_exists('1', $element->children[2]->children)){
    			$proc = $element->children[2]->children[1]->src. ' ';    		
    			$proc=$this->massReplace($proc);
    			$procenatFix1 = $procenatFix1.$proc;
    		}
    	
    		else {
    			$proc = $element->children[2];    			
    			$procenatFix1 = $procenatFix1.$proc;
    		}
    		
    		$procenatFix1=trim($procenatFix1);
    		
    		// *************
    		
    		$procenatFixX="";
    		if(array_key_exists('0', $element->children[3]->children)){
    			$proc = $element->children[3]->children[0]->src;
    		
    			$proc=$this->massReplace($proc);
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		else {
    			$proc = $element->children[3];
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		 
    		if(array_key_exists('1', $element->children[3]->children)){
    			$proc = $element->children[3]->children[1]->src. ' ';
    			$proc=$this->massReplace($proc);
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		 
    		else {
    			$proc = $element->children[3];
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		
    		$procenatFixX=trim($procenatFixX);
    		// *************
    		
    		$procenatFix2="";
    		if(array_key_exists('0', $element->children[4]->children)){
    			$proc = $element->children[4]->children[0]->src;
    		
    			$proc=$this->massReplace($proc);
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		else {
    			$proc = $element->children[4];
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		 
    		if(array_key_exists('1', $element->children[4]->children)){
    			$proc = $element->children[4]->children[1]->src. ' ';
    			$proc=$this->massReplace($proc);
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		 
    		else {
    			$proc = $element->children[4];
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		
    		$procenatFix2=trim($procenatFix2);
    		
    		// parsing of tips    		
    		$tip=$this->parseTip($element->children[5]->children[0]->src);
			// parsing odds
    		$odds1= $element->children[6]->plaintext;
    		$oddsX= $element->children[7]->plaintext;
    		$odds2= $element->children[8]->plaintext;
    		// other parse
    		$score1 = $element->children[9]->plaintext;
    		$score2 = $element->children[10]->plaintext;
    		$under = $element->children[11]->plaintext;
    		$over = $element->children[12]->plaintext;
    		$result = $element->children[13]->plaintext;
    		
    		
    		$game->initialise($element->children[0]->plaintext,$teams[0],$teams[1],$procenatFix1,$procenatFixX,$procenatFix2,$tip,$odds1,$oddsX,$odds2,$score1,$score2,$under,$over,$result);
    		$game->setDateAndRow($dateFromPage,$rowNo);
    		$rowNo++;
    		$arrayOfGames[$i]=$game;
    		$i++;
    		$procenatFix1="";
    		$procenatFixX="";
    		$procenatFix2="";
    			
    	}
    	
    	/////////////////////
    	$arrayOfF1=$raw->find('tr[class=f1]');
    	array_pop($arrayOfF1);
    	foreach($arrayOfF1 as $element){
    		//echo $element->outertext . '<br>';
    		
    		$game= new Game();
    		// parsiranje timova
    		$teams = $this->parseTeams($element->children[1]->plaintext);
    		// parsiranje procenata
    		$procenatFix1="";
    		if(array_key_exists('0', $element->children[2]->children)){
    			$proc = $element->children[2]->children[0]->src;
    	
    			$proc=$this->massReplace($proc);
    			$procenatFix1 = $procenatFix1.$proc;
    		}
    		else {
    			$proc = $element->children[2];
    			$procenatFix1 = $procenatFix1.$proc;
    		}
    		 
    		if(array_key_exists('1', $element->children[2]->children)){
    			$proc = $element->children[2]->children[1]->src. ' ';
    			$proc=$this->massReplace($proc);
    			$procenatFix1 = $procenatFix1.$proc;
    		}
    		 
    		else {
    			$proc = $element->children[2];
    			$procenatFix1 = $procenatFix1.$proc;
    		}
    	
    		$procenatFix1=trim($procenatFix1);
    	
    		// *************
    	
    		$procenatFixX="";
    		if(array_key_exists('0', $element->children[3]->children)){
    			$proc = $element->children[3]->children[0]->src;
    	
    			$proc=$this->massReplace($proc);
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		else {
    			$proc = $element->children[3];
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		 
    		if(array_key_exists('1', $element->children[3]->children)){
    			$proc = $element->children[3]->children[1]->src. ' ';
    			$proc=$this->massReplace($proc);
    			$procenatFixX = $procenatFixX.$proc;
    		}
    		 
    		else {
    			$proc = $element->children[3];
    			$procenatFixX = $procenatFixX.$proc;
    		}
    	
    		$procenatFixX=trim($procenatFixX);
    		// *************
    	
    		$procenatFix2="";
    		if(array_key_exists('0', $element->children[4]->children)){
    			$proc = $element->children[4]->children[0]->src;
    	
    			$proc=$this->massReplace($proc);
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		else {
    			$proc = $element->children[4];
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		 
    		if(array_key_exists('1', $element->children[4]->children)){
    			$proc = $element->children[4]->children[1]->src. ' ';
    			$proc=$this->massReplace($proc);
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    		 
    		else {
    			$proc = $element->children[4];
    			$procenatFix2 = $procenatFix2.$proc;
    		}
    	
    		$procenatFix2=trim($procenatFix2);
    	
    		// parsing of tips
    		$tip=$this->parseTip($element->children[5]->children[0]->src);
    		// parsing odds
    		$odds1= $element->children[6]->plaintext;
    		$oddsX= $element->children[7]->plaintext;
    		$odds2= $element->children[8]->plaintext;
    		// other parse
    		$score1 = $element->children[9]->plaintext;
    		$score2 = $element->children[10]->plaintext;
    		$under = $element->children[11]->plaintext;
    		$over = $element->children[12]->plaintext;
    		$result = $element->children[13]->plaintext;
    	
    	
    		$game->initialise($element->children[0]->plaintext,$teams[0],$teams[1],$procenatFix1,$procenatFixX,$procenatFix2,$tip,$odds1,$oddsX,$odds2,$score1,$score2,$under,$over,$result);
    		$game->setDateAndRow($dateFromPage,$rowNo);
    		$arrayOfGames[$i]=$game;
    		$i++;
    		$rowNo++;
    		$procenatFix1="";
    		$procenatFixX="";
    		$procenatFix2="";
    		 
    	}
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	/*
    	foreach($raw->find('tr[class=f1]') as $element){
    		
    		$game= new Game();
    		
    		if(array_key_exists('1', $element->children)){
    		$teams = explode(" - ", $element->children[1]->plaintext);
    		}
    		else {
    			$teams[0]="error";
    			$teams[1]="error";
    		} 	

    		// parsiranje procenata
    		$procenatFix1="";
    		
    		if (array_key_exists('2', $element->children)){
	    		if(array_key_exists('0', $element->children[2]->children)){
	    			$proc = $element->children[2]->children[0]->src;
	    			//echo $proc. '<br>';
	    			$proc=$this->massReplace($proc);
	    			$procenatFix1 = $procenatFix1.$proc;
	    		}
	    		else {
	    			$proc = $element->children[2];
	    			$procenatFix1 = $procenatFix1.$proc;
	    		}
    		}

    		if (array_key_exists('2', $element->children)){
	    		if(array_key_exists('1', $element->children[2]->children)){
	    			$proc = $element->children[2]->children[1]->src. ' ';
	    			$proc=$this->massReplace($proc);
	    			$procenatFix1 = $procenatFix1.$proc;
	    		}
	    		 
	    		else {
	    			$proc = $element->children[2];
	    			$procenatFix1 = $procenatFix1.$proc;
	    		}
    		}
    		
    		$procenatFix1=trim($procenatFix1);
    		
    		// parsing odds
    		if (array_key_exists('6', $element))
    		$odds1= $element->children[6]->plaintext;
    		if (array_key_exists('7', $element))
    		$oddsX= $element->children[7]->plaintext;
    		if (array_key_exists('8', $element))
    		$odds2= $element->children[8]->plaintext;
    		// other parse
    		if (array_key_exists('9', $element))
    		$score1 = $element->children[9]->plaintext;
    		if (array_key_exists('10', $element))
    		$score2 = $element->children[10]->plaintext;
    		if (array_key_exists('11', $element))
    		$under = $element->children[11]->plaintext;
    		if (array_key_exists('12', $element))
    		$over = $element->children[12]->plaintext;
    		if (array_key_exists('13', $element))
    		$result = $element->children[13]->plaintext;
    		
    		$game->initialise($element->children[0]->plaintext,$teams[0],$teams[1],$procenatFix1,'','','',$odds1,$oddsX,$odds2,$score1,$score2,$under,$over,$result);
    		$arrayOfGames[$i]=$game;
    		$i++;
    		$procenatFix1='';
    	}*/
    	// remove last item in array, not needed "back to top"
    	//array_pop($arrayOfGames);    	
    	
    	return $arrayOfGames;
    	
    }
}

/* End of file Someclass.php */

?>